export let pathMap={}
function createRouteMap(routes=[], parentObj) {
  routes.forEach(route => {
    let path = parentObj ? parentObj.path == "/" ? `/${route.path}` : `${parentObj.path}/${route.path}` : route.path
    let record = {
      path,
      name: route.name,
      component: route.component,
      meta: route.meta,
      parent: parentObj
    }
    if (!pathMap[path]) {
      pathMap[path] = record
    }
    route.children && createRouteMap(route.children, record)
  })
}
export default function createMacher(routes) {
  createRouteMap(routes)
  function addRoutes(_routes) {
    createRouteMap(_routes)
    console.log(pathMap, 2000);
  }
  function addRoute(route) {
    createRouteMap([route])
    console.log(pathMap, 5000);
  }
  function match(path) {
    return pathMap[path]
  }
  return {
    addRoutes,
    addRoute,
    match
  }
}